<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="4" :labelWidth="130" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <BaseDialog :visible.sync="ismonitoring" title="配件车型车系" width="950" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <partModel @close="closeHandler" />
      </BaseDialog>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" :loading="confirmBtnLoading" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
    <!-- 替换件选择弹框 -->
    <BaseDialog :visible.sync="replacePartSelectVisible" title="选择替换件" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <replacePartSelect @close="replacePartSelectVisible = false" @onConfirm="handleReplacePartConfirm" />
    </BaseDialog>
    <!-- 车型选择弹窗 -->
    <BaseDialog :visible.sync="modelSelectVisible" title="选择车型" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <modelSelect :seriesId="querySeriesId" @close="modelSelectVisible = false" @onConfirm="handleBrandSelectConfirm" />
    </BaseDialog>
  </div>
</template>

<script>
import { addPartInfo, ModifyPartInfo, queryUnitCode, queryPackUnit } from '@/api/part/partInfo';
import { getBrand } from '@/api/repair/basicParameters';
import { dictionary } from '@/utils/dictMixin';
import { getBrandAll, queryTex } from '@/api/part/common';
import { getSeriesData, getModelData, getBrandList } from '@/api/common/cars/brandcarsModels';
import { notifyAction } from '@/utils';

import partModel from './partModel';
import replacePartSelect from './replacePartSelect';
import modelSelect from './modelSelect';

// let pinyin = require('js-pinyin');
// pinyin.setOptions({ checkPolyphone: false, charCase: 0 });
import pinyin, { STYLE_FIRST_LETTER } from '@/components/Pinyin/index';
import { getPinpSelect, getChexSelect, getCheModelSelect } from '@/api/repair/repairReception/customer';

export default {
  name: 'createPart',
  components: {
    partModel,
    replacePartSelect,
    modelSelect
  },
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改', show: '明细' };
    this.saveData = null;
    return {
      pateVehicleModel: '',
      ismonitoring: false,
      formList: this.createFormList(),
      replacePartSelectVisible: false, // 是否显示替换件选择弹框
      confirmBtnLoading: false,
      brandId: '',
      querySeriesId: '', // 选中的车系id(用于查询)
      modelSelectVisible: false, // 是否显示车型弹窗
      modelSelectData: {},
      tax: 0
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
    this.getBrand();
    this.getUnit();
    // this.queryPackUnitList();
    // 编辑初始化
    if (this.rowData.brand) {
      this.getSeries(this.rowData.brandId);
    }
    // if (this.rowData.series) {
    //   this.getModel(this.rowData.series);
    // }
  },
  methods: {
    init() {
      if (this.type !== 'add') {
        // 更新接口需要的记录 ID 编辑赋值
        this.recordId = this.rowData.partNo;
        let transitionList = ['partSortCode', 'packageUnit']; // 需要手动转换为数字类型的select数据回显值
        this.formList.forEach(x => {
          if (transitionList.indexOf(x.fieldName) > -1) {
            x.initialValue = this.rowData[x.fieldName] * 1;
          } else {
            x.initialValue = this.rowData[x.fieldName];
          }
        });
        this.formList.find(item => item.fieldName == 'partNo').disabled = true;
        this.formList.find(item => item.fieldName == 'spellCode').disabled = true;
        this.queryModelName(this.rowData.series, this.rowData.model);

        // 原厂件禁用abc是否附件字段编辑
        if (this.rowData.downTag == 10041001) {
          this.formList.find(item => item.fieldName == 'abcType').disabled = true;
          this.formList.find(item => item.fieldName == 'isAccessory').disabled = true;
        }
      } else if (this.type == 'add' && this.rowData.partNo) {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          selfCols: 2,
          placeholder: '请输入...',
          // rules: [{ required: true, message: '请输入配件代码', trigger: 'blur' }]
          rules: [{ required: true, validator: this.checkPartNo, trigger: 'blur', message: '请正确输入配件代码' }]
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partDescription',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件说明',
          fieldName: 'partName',
          placeholder: '请输入...',
          onInput: val => {
            this.formList.find(item => item.fieldName == 'spellCode').initialValue = pinyin(val, { style: STYLE_FIRST_LETTER })
              .flat()
              .join('')
              .toUpperCase();
          },
          rules: [{ required: true, message: '请输入配件说明', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '配件类别',
          fieldName: 'partGroupCode',
          initialValue: 80751001,
          itemList: this.createDictList('8075')
        },
        {
          type: 'SELECT',
          label: '类型',
          fieldName: 'partType',
          placeholder: '请选择',
          initialValue: 50001002,
          itemList: this.createDictList('5000'),
          rules: [{ required: true, message: '请选择类型', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '安全库存',
          fieldName: 'safeStock',
          precision: 2,
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入安全库存', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'spellCode',
          placeholder: '请输入...',
          initialValue: '',
          readonly: true,
          rules: [{ required: false, message: '请输入拼音代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '储备高限',
          isControls: false,
          fieldName: 'maxStock',
          precision: 2,
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入最大库存', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '储备低限',
          fieldName: 'minStock',
          precision: 2,
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入最小库存', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '单位',
          fieldName: 'unitCode',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择单位', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '包装单位',
          fieldName: 'packageUnit',
          placeholder: '请输入...',
          itemList: this.createDictList('8105'),
          rules: [{ required: false, message: '请输入包装单位', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '替代配件',
          fieldName: 'optionNo',
          placeholder: '请选择...',
          readonly: true,
          initialValue: '',
          unitRender: params => {
            return <el-button icon="el-icon-search" onClick={() => this.searchReplacePart()}></el-button>;
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '产地',
        //   fieldName: 'oriProCode',
        //   placeholder: '请输入...',
        //   rules: [{ required: false, message: '请输入产地', trigger: 'blur' }]
        // },

        // {
        //   type: 'INPUT_NUMBER',
        //   isControls: false,
        //   label: '网点价',
        //   fieldName: 'nodePrice',
        //   precision: 2,
        //   placeholder: '请输入...',
        //   rules: [{ required: false, message: '请输入网点价', trigger: 'blur' }]
        // },

        {
          type: 'SELECT',
          label: 'ABC分类',
          fieldName: 'abcType',
          placeholder: '请选择',
          itemList: this.createDictList('8092'),
          rules: [{ required: false, message: '请选择ABC分类', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '配件分类代码',
          fieldName: 'partSortCode',
          placeholder: '请输入...',
          itemList: this.createDictList(8106),
          rules: [{ required: false, message: '请输入配件分类代码', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'series',
          placeholder: '请输入...',
          change: val => {
            this.changeSeriesSelect(val);
          },
          itemList: [],
          rules: [{ required: false, message: '请输入车系', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'model',
          placeholder: '请输入...',
          itemList: [],
          readonly: true,
          initialValue: '',
          rules: [{ required: false, message: '请输入车型', trigger: 'blur' }],
          unitRender: params => {
            return <el-button icon="el-icon-search" onClick={() => this.selectCarModel()}></el-button>;
          }
        },

        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '不含税采购单价',
          fieldName: 'planPrice',
          precision: 2,
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入采购单价', trigger: 'blur' }],
          change: e => {
            this.$nextTick(() => {
              this.reComputedsalerPrice();
            });
          }
        },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '加价率',
          fieldName: 'priceAddRate',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入加价率', trigger: 'blur' }],
          min: 0,
          max: 100,
          change: e => {
            this.$nextTick(() => {
              this.reComputedsalerPrice();
            });
          }
        },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '含税销售单价',
          fieldName: 'costSalesPrice',
          precision: 2,
          placeholder: '请输入...',
          initialValue: 0,
          rules: [{ required: false, message: '请输入销售单价', trigger: 'blur' }]
        },
        // {
        //   type: 'INPUT_NUMBER',
        //   label: '常规订单价格',
        //   isControls: false,
        //   fieldName: 'regularPrice',
        //   precision: 2,
        //   placeholder: '请输入...',
        //   // pattern: /^[1-9]{1}[0-9]*\.?([0-9]{1,2})?$/,
        //   rules: [
        //     { required: false, message: '请输入常规订单价格', trigger: 'blur' }
        //     // { pattern: /^[1-9]{1}[0-9]*(\.[0-9]{1,2})?$/, message: '请输入正确的价格', trigger: 'blur' }
        //   ]
        // },
        // {
        //   type: 'INPUT_NUMBER',
        //   isControls: false,
        //   label: '销售限价',
        //   fieldName: 'limitPrice',
        //   precision: 2,
        //   placeholder: '请输入...',
        //   rules: [{ required: false, message: '请输入销售限价', trigger: 'blur' }]
        // },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '含税批发单价',
          fieldName: 'costWholesalesPrice',
          precision: 2,
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入批发单价', trigger: 'blur' }]
        },
        // {
        //   type: 'INPUT_NUMBER',
        //   isControls: false,
        //   label: '含税出库单价',
        //   fieldName: 'costOutPrice',
        //   precision: 2,
        //   placeholder: '请输入...',
        //   rules: [{ required: false, message: '请输入出库单价', trigger: 'blur' }]
        // },
        // {
        //   type: 'INPUT_NUMBER',
        //   isControls: false,
        //   label: '索赔价',
        //   fieldName: 'claimPrice',
        //   precision: 2,
        //   placeholder: '请输入...',
        //   rules: [{ required: false, message: '请输入索赔价', trigger: 'blur' }]
        // },
        // {
        //   type: 'INPUT_NUMBER',
        //   isControls: false,
        //   label: '终端建议零售价格',
        //   fieldName: 'insurancePrice',
        //   precision: 2,
        //   placeholder: '请输入...',
        //   rules: [{ required: false, message: '请输入终端建议零售价格', trigger: 'blur' }]
        // },

        {
          type: 'SELECT',
          label: '激活停用',
          fieldName: 'isActivate',
          placeholder: '请选择',
          itemList: this.createDictList('8131'),
          change: e => {
            if (e == 81311002) {
              this.formList.find(item => item.fieldName == 'activateDate').initialValue = new Date();
            } else {
              this.formList.find(item => item.fieldName == 'activateDate').initialValue = '';
            }
          },
          rules: [{ required: false, message: '请选择激活停用', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '激活停用时间',
          fieldName: 'activateDate',
          valueFormat: 'yyyy-MM-dd',
          initialValue: '',
          disabled: true
          // rules: [{ required: true, message: '激活停用时间', trigger: 'change' }]
        },
        {
          type: 'CHECKBOX',
          label: '成套件',
          fieldName: 'isKitPart',
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          },
          rules: [{ required: false, message: '请选择成套件', trigger: 'change' }],
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '是否附件',
          fieldName: 'isAccessory',
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          },
          rules: [{ required: false, message: '请选择是否附件', trigger: 'change' }],
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '套餐',
          fieldName: 'isPackage',
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          },
          rules: [{ required: false, message: '请选择套餐', trigger: 'change' }],
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '二维码激活',
          fieldName: 'qrActive',
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          },
          rules: [{ required: false, message: '请选择二维码激活', trigger: 'change' }],
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '常用件',
          fieldName: 'isCommonPart',
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          },
          rules: [{ required: false, message: '请选择常用件', trigger: 'change' }],
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: 'FIB标识',
          fieldName: 'fibMark',
          disabled: true,
          options: {
            trueValue: 'X',
            falseValue: 0
          },
          selfCols: 0.5
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入备注', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '呆滞天数',
          fieldName: 'sluggishDays',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入呆滞天数', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '九大类',
          fieldName: 'partMainType',
          placeholder: '请选择',
          itemList: this.createDictList('8094'),
          rules: [{ required: false, message: '请选择九大类', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          placeholder: '请选择',
          change: val => {
            this.changeBrandSelect(val);
          },
          itemList: [],
          rules: [{ required: true, message: '请选择品牌', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '停用',
          fieldName: 'isDisable',
          placeholder: '请选择',
          itemList: this.createDictList('8131'),
          initialValue: 81311001,
          change: e => {
            if (e == 81311002) {
              this.formList.find(item => item.fieldName == 'disableDate').initialValue = new Date();
            } else {
              this.formList.find(item => item.fieldName == 'disableDate').initialValue = '';
            }
          },
          rules: [{ required: false, message: '请选择激活停用', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '停用时间',
          fieldName: 'disableDate',
          valueFormat: 'yyyy-MM-dd',
          initialValue: '',
          disabled: true
          // rules: [{ required: true, message: '激活停用时间', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: 'CCC件',
          fieldName: 'isCccPart',
          placeholder: '请选择',
          itemList: this.createDictList('8099'),
          disabled: true,
          rules: [{ required: false, message: 'SELECT', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '体积',
          fieldName: 'volume',
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '请输入体积', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '体积单位',
          fieldName: 'volumeUnit',
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '体积单位', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '体积标识',
          fieldName: 'volumeMark',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          disabled: true,
          rules: [{ required: false, message: '体积标识', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '佣金组',
          fieldName: 'commissGroup',
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '佣金组', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '厂家建议不含税零售价',
          fieldName: 'referPrice',
          precision: 2,
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '厂家建议零售价', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '开口件代码',
          fieldName: 'openCode',
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '开口件代码', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '新能源标识',
          fieldName: 'isEnergy',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          disabled: true,
          rules: [{ required: false, message: '新能源标识', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '重量',
          fieldName: 'weight',
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '重量', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '重量单位',
          fieldName: 'weightUnit',
          placeholder: '请输入...',
          disabled: true,
          rules: [{ required: false, message: '重量单位', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '重量标识',
          fieldName: 'weightMark',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          disabled: true,
          rules: [{ required: false, message: '重量标识', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          isControls: false,
          label: '最小包装数',
          fieldName: 'packageLiaght',
          precision: 2,
          disabled: true,
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入最小包装数', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修电动汽车标识',
          fieldName: 'repairVehicleMark',
          disabled: true
        },
        {
          type: 'CHECKBOX',
          label: '保质期标识',
          fieldName: 'shelfLiftMark',
          disabled: true,
          options: {
            trueValue: '1',
            falseValue: '0'
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '是否在EOP',
          fieldName: 'isEop',
          disabled: true,
          options: {
            trueValue: 'x',
            falseValue: 0
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '采购锁标识',
          fieldName: 'stockLockMark',
          disabled: true,
          options: {
            trueValue: '1',
            falseValue: '0'
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '销售锁标识',
          fieldName: 'saleLockMark',
          disabled: true,
          options: {
            trueValue: '1',
            falseValue: '0'
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '仅辖区库发货标识',
          fieldName: 'shipMark',
          disabled: true,
          options: {
            trueValue: 'x',
            falseValue: 0
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '个性化件',
          fieldName: 'personOrderMark',
          disabled: true,
          options: {
            trueValue: 'X',
            falseValue: 0
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '三方订单件',
          fieldName: 'thirdOrderMark',
          disabled: true,
          options: {
            trueValue: 'X',
            falseValue: 0
          },
          selfCols: 0.5
        },
        {
          type: 'CHECKBOX',
          label: '原厂标识',
          fieldName: 'downTag',
          disabled: true,
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          },
          selfCols: 0.5
        },
        {
          type: 'INPUT',
          label: '加价组',
          fieldName: 'groupMark',
          disabled: true
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
      this.saveData.pateVehicleModel = this.pateVehicleModel;
    },
    // 点击保存
    async saveDialog() {
      // this.$refs.formPanel.SUBMIT_FORM();
      let formData = await this.$refs.formPanel.GET_FORM_DATA();
      if (formData[0]) {
        let keys = Object.keys(formData[0]);
        console.log(keys);
        notifyAction(formData[0][keys[0]][0].message, 'warning');
        return;
      } else {
        this.saveData = formData[1];
        this.saveData.pateVehicleModel = this.pateVehicleModel;
      }

      if (this.saveData !== null) {
        if (this.saveData.maxStock < this.saveData.minStock) {
          this.$notify.error({ title: '提示信息', message: '配件的最大库存小于最小库存!' });
          return;
        }

        // 车型提交单独处理
        this.saveData.model = this.modelSelectData.modelId;

        if (this.confirmBtnLoading) return;
        this.confirmBtnLoading = true;
        let res = {};
        try {
          if (this.type === 'add') {
            res = await addPartInfo(this.saveData);
          }
          if (this.type === 'edit') {
            res = await ModifyPartInfo(this.saveData, this.recordId);
          }
        } catch (err) {
          console.log(err, '保存失败');
        } finally {
          this.confirmBtnLoading = false;
        }
        if (res.resultCode == '200') {
          this.test();
        }
      }
    },
    checkPartNo(rule, value, callback) {
      if (!value) {
        return callback(new Error(rule.message));
      } else {
        if (!/[\u4E00-\u9FA5]/g.test(value)) {
          callback();
        } else {
          return callback(new Error(rule.message));
        }
      }
      callback();
    },
    // 查询所有品牌
    async getBrand() {
      const res = await getBrand();
      let brandItem = this.formList.find(x => x.fieldName === 'brand');
      brandItem.itemList = res.data.map(x => ({ text: x.BRAND_NAME, value: x.BRAND_CODE + '', id: x.BRAND_ID }));
      brandItem.initialValue = res.data.find(item => item.BRAND_NAME == '奥迪').BRAND_CODE + '';
      // 初始化车系列表
      this.getSeries();
    },
    // 根据品牌查询车系
    async getSeries(brandId) {
      let brandItem = this.formList.find(x => x.fieldName === 'brand');
      let quertBrandId = brandId || brandItem.itemList.find(item => item.value == brandItem.initialValue) ? brandItem.itemList.find(item => item.value == brandItem.initialValue).id : false;
      if (quertBrandId == false) return;
      let res = await getSeriesData(quertBrandId);
      if (res.resultCode == 200) {
        this.formList.find(item => item.fieldName == 'series').itemList = res.data.map(item => ({ text: item.seriesName, value: item.seriesId + '' }));
      }
    },
    // 根据车系查询车型
    async getModel(seriesId) {
      let querySeriesId = seriesId || this.formList.find(x => x.fieldName === 'series').initialValue;
      const res = await getModelData(querySeriesId);
      this.formList.find(x => x.fieldName === 'model').itemList = res.data.map(x => ({ value: x.modelId + '', text: x.modelName }));
    },
    // 翻译车型名称
    async queryModelName(seriesId, modelId) {
      if (seriesId && modelId) {
        let res = await getModelData(seriesId);
        if (res.resultCode == 200) {
          let modelItem = res.data.find(item => item.modelId == modelId);
          this.modelSelectData = {
            modelName: modelItem.modelName,
            modelId: modelItem.modelId
          };
          this.formList.find(item => item.fieldName == 'model').initialValue = modelItem.modelName;
        }
      }
    },
    // 品牌下拉框改变时
    changeBrandSelect(brandId) {
      // console.log(brandId, '品牌改变');
      this.clearFormItem(this.formList.find(x => x.fieldName === 'series'));
      this.clearFormItem(this.formList.find(x => x.fieldName === 'model'));
      if (!brandId) return;
      this.getSeries(brandId);
    },
    // 车系下拉框改变时
    changeSeriesSelect(seriesId) {
      this.clearFormItem(this.formList.find(x => x.fieldName === 'model'));
      this.modelSelectData = {};
      // if (!seriesId) return;
      // this.getModel(seriesId);
    },
    // 清空下拉框表当控件的值和下拉列表
    clearFormItem(item) {
      item.initialValue = '';
      item.itemList = [];
    },
    async getUnit() {
      const res = await queryUnitCode();
      this.formList.find(item => item.fieldName == 'unitCode').itemList = res.data.map(x => ({ text: x.UNIT_NAME, value: x.UNIT_CODE }));
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    monitoringFun() {
      this.ismonitoring = true;
    },
    closeHandler(val) {
      if (val) {
        this.formList[10].initialValue = val.name;
        this.pateVehicleModel = val.code;
        this.formList = [...this.formList];
      }
      this.ismonitoring = false;
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
      this.closeDialog('refresh');
    },
    // 点击替换件搜索按钮
    searchReplacePart() {
      this.replacePartSelectVisible = true;
    },
    // 替换件选择确定
    handleReplacePartConfirm(e) {
      this.formList.find(item => item.fieldName == 'optionNo').initialValue = e.partNo;
      console.log(this.formList.find(item => item.fieldName == 'optionNo').initialValue);
    },
    // 查询包装单位列表
    async queryPackUnitList() {
      let res = await queryPackUnit();
      this.formList.find(item => item.fieldName == 'packageUnit').itemList = res.data.map(item => ({ text: item.UNIT_NAME, value: item.UNIT_CODE }));
    },
    // 重新计算销售价
    async reComputedsalerPrice() {
      let noTaxPrice = this.formList.find(item => item.fieldName == 'planPrice').initialValue;
      let priceAddRate = this.formList.find(item => item.fieldName == 'priceAddRate').initialValue;
      let salePriceItem = this.formList.find(item => item.fieldName == 'costSalesPrice');
      try {
        let res = await queryTex();
        this.tax = res.data || 0.13;
      } catch (err) {
        console.log(err, '获取税率失败');
      }
      console.log('重新计算销售价', noTaxPrice, priceAddRate, salePriceItem);
      if (noTaxPrice) {
        if (priceAddRate) {
          salePriceItem.initialValue = noTaxPrice * (1 + (+this.tax || 0.13)) * (1 + priceAddRate);
        } else {
          salePriceItem.initialValue = noTaxPrice;
        }
      }
    },
    // 点击车型选择
    selectCarModel() {
      let querySeriesId = this.formList.find(x => x.fieldName === 'series').initialValue;
      if (querySeriesId) {
        this.querySeriesId = querySeriesId;
        this.modelSelectVisible = true;
      } else {
        this.$notify.warning('请先选择车系');
      }
    },
    // 车型选择点击确定
    handleBrandSelectConfirm(e) {
      console.log(e);
      this.modelSelectData = e;
      this.formList.find(item => item.fieldName == 'model').initialValue = e.modelName;
      console.log(this.formList.find(item => item.fieldName == 'model'));
    }
  }
};
</script>

<style lang="less" scoped></style>
